import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: Home,
    redirect: '/helloworld',
    children:[
      {
        path: '/helloworld',
        name: 'Helloworld',
        component: () => import('../components/HelloWorld.vue')
      },
      {
        path: '/users',
        name: 'User',
        component:()=>import("../components/user/Users.vue")
      },
      {
        path: '/rights',
        name: 'Rights',
        component:()=>import("../components/power/Rights.vue")
      },
      {
        path: '/Roles',
        name: 'roles',
        component:()=>import("../components/power/Roles.vue")
      }
    ]
  }
  
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if(to.path=='/login'){
    next()
  }else{
    const token = window.sessionStorage.getItem('token');
    if(!token){
      next('/login')
    }else{
      next()
    }
  }
})

export default router
